/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ImgPreviewPanel.java
 *
 * Created on 10-jun-2010, 23:38:21
 */

package tp_imag.gui.group.processing;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.image.BufferedImage;
import java.util.List;
import javax.swing.JPanel;
import tp_imag.gui.Tp_imagView;
import tp_imag.gui.components.JSelectImagePanel;
import tp_imag.gui.components.SelectImageListener;
import tp_imag.model.ImageItem;
import tp_imag.service.ImageProcessor;
import tp_imag.service.ServiceFactory;

/**
 *
 * @author Administrador
 */
public class ImgPreviewPanel extends javax.swing.JPanel implements SelectImageListener {

    private final static int IMAGE_CANT = 16;

    private JPanel[] panelImages = new JPanel[IMAGE_CANT];

    /* servicio */
    private ImageProcessor serv = ServiceFactory.getInstance().getImageProcessorService();
    
    /* Main form of the application */
    private Tp_imagView mainForm;
    

    /** Creates new form ImgPreviewPanel */
    public ImgPreviewPanel(Tp_imagView mainForm) {
        initComponents();
        initImages();

        this.mainForm = mainForm;
    }

    public void cleanImages() {
        for (int i = 0; i < IMAGE_CANT; i++) {
            panelImages[i].removeAll();
        }
    }

    public void refreshImages(List<ImageItem> imageItems) {

        cleanImages();

        int i = 0;
        for (ImageItem item : imageItems) {            
            BufferedImage img = serv.openImage(item.getBinarizedImagePath());
            loadImage(i, img, item);
            i++;
        }
        
    }


    private void initImages() {
        panelImages[0] = jPanelImg1;
        panelImages[1] = jPanelImg2;
        panelImages[2] = jPanelImg3;
        panelImages[3] = jPanelImg4;
        panelImages[4] = jPanelImg5;
        panelImages[5] = jPanelImg6;
        panelImages[6] = jPanelImg7;
        panelImages[7] = jPanelImg8;
        panelImages[8] = jPanelImg9;
        panelImages[9] = jPanelImg10;
        panelImages[10] = jPanelImg11;
        panelImages[11] = jPanelImg12;
        panelImages[12] = jPanelImg13;
        panelImages[13] = jPanelImg14;
        panelImages[14] = jPanelImg15;
        panelImages[15] = jPanelImg16;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel9 = new javax.swing.JPanel();
        jPanelImg1 = new javax.swing.JPanel();
        jPanelImg2 = new javax.swing.JPanel();
        jPanelImg3 = new javax.swing.JPanel();
        jPanelImg4 = new javax.swing.JPanel();
        jPanelImg5 = new javax.swing.JPanel();
        jPanelImg6 = new javax.swing.JPanel();
        jPanelImg7 = new javax.swing.JPanel();
        jPanelImg8 = new javax.swing.JPanel();
        jPanelImg9 = new javax.swing.JPanel();
        jPanelImg10 = new javax.swing.JPanel();
        jPanelImg11 = new javax.swing.JPanel();
        jPanelImg12 = new javax.swing.JPanel();
        jPanelImg13 = new javax.swing.JPanel();
        jPanelImg14 = new javax.swing.JPanel();
        jPanelImg15 = new javax.swing.JPanel();
        jPanelImg16 = new javax.swing.JPanel();

        jPanel9.setName("jPanel9"); // NOI18N

        setLayout(new java.awt.GridLayout(4, 4, 8, 8));

        jPanelImg1.setName("jPanelImg1"); // NOI18N

        javax.swing.GroupLayout jPanelImg1Layout = new javax.swing.GroupLayout(jPanelImg1);
        jPanelImg1.setLayout(jPanelImg1Layout);
        jPanelImg1Layout.setHorizontalGroup(
            jPanelImg1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg1Layout.setVerticalGroup(
            jPanelImg1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg1);

        jPanelImg2.setName("jPanelImg2"); // NOI18N

        javax.swing.GroupLayout jPanelImg2Layout = new javax.swing.GroupLayout(jPanelImg2);
        jPanelImg2.setLayout(jPanelImg2Layout);
        jPanelImg2Layout.setHorizontalGroup(
            jPanelImg2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg2Layout.setVerticalGroup(
            jPanelImg2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg2);

        jPanelImg3.setName("jPanelImg3"); // NOI18N

        javax.swing.GroupLayout jPanelImg3Layout = new javax.swing.GroupLayout(jPanelImg3);
        jPanelImg3.setLayout(jPanelImg3Layout);
        jPanelImg3Layout.setHorizontalGroup(
            jPanelImg3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg3Layout.setVerticalGroup(
            jPanelImg3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg3);

        jPanelImg4.setName("jPanelImg4"); // NOI18N

        javax.swing.GroupLayout jPanelImg4Layout = new javax.swing.GroupLayout(jPanelImg4);
        jPanelImg4.setLayout(jPanelImg4Layout);
        jPanelImg4Layout.setHorizontalGroup(
            jPanelImg4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg4Layout.setVerticalGroup(
            jPanelImg4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg4);

        jPanelImg5.setName("jPanelImg5"); // NOI18N

        javax.swing.GroupLayout jPanelImg5Layout = new javax.swing.GroupLayout(jPanelImg5);
        jPanelImg5.setLayout(jPanelImg5Layout);
        jPanelImg5Layout.setHorizontalGroup(
            jPanelImg5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg5Layout.setVerticalGroup(
            jPanelImg5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg5);

        jPanelImg6.setName("jPanelImg6"); // NOI18N

        javax.swing.GroupLayout jPanelImg6Layout = new javax.swing.GroupLayout(jPanelImg6);
        jPanelImg6.setLayout(jPanelImg6Layout);
        jPanelImg6Layout.setHorizontalGroup(
            jPanelImg6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg6Layout.setVerticalGroup(
            jPanelImg6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg6);

        jPanelImg7.setName("jPanelImg7"); // NOI18N

        javax.swing.GroupLayout jPanelImg7Layout = new javax.swing.GroupLayout(jPanelImg7);
        jPanelImg7.setLayout(jPanelImg7Layout);
        jPanelImg7Layout.setHorizontalGroup(
            jPanelImg7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg7Layout.setVerticalGroup(
            jPanelImg7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg7);

        jPanelImg8.setName("jPanelImg8"); // NOI18N

        javax.swing.GroupLayout jPanelImg8Layout = new javax.swing.GroupLayout(jPanelImg8);
        jPanelImg8.setLayout(jPanelImg8Layout);
        jPanelImg8Layout.setHorizontalGroup(
            jPanelImg8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg8Layout.setVerticalGroup(
            jPanelImg8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg8);

        jPanelImg9.setName("jPanelImg9"); // NOI18N

        javax.swing.GroupLayout jPanelImg9Layout = new javax.swing.GroupLayout(jPanelImg9);
        jPanelImg9.setLayout(jPanelImg9Layout);
        jPanelImg9Layout.setHorizontalGroup(
            jPanelImg9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg9Layout.setVerticalGroup(
            jPanelImg9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg9);

        jPanelImg10.setName("jPanelImg10"); // NOI18N

        javax.swing.GroupLayout jPanelImg10Layout = new javax.swing.GroupLayout(jPanelImg10);
        jPanelImg10.setLayout(jPanelImg10Layout);
        jPanelImg10Layout.setHorizontalGroup(
            jPanelImg10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg10Layout.setVerticalGroup(
            jPanelImg10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg10);

        jPanelImg11.setName("jPanelImg11"); // NOI18N

        javax.swing.GroupLayout jPanelImg11Layout = new javax.swing.GroupLayout(jPanelImg11);
        jPanelImg11.setLayout(jPanelImg11Layout);
        jPanelImg11Layout.setHorizontalGroup(
            jPanelImg11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg11Layout.setVerticalGroup(
            jPanelImg11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg11);

        jPanelImg12.setName("jPanelImg12"); // NOI18N

        javax.swing.GroupLayout jPanelImg12Layout = new javax.swing.GroupLayout(jPanelImg12);
        jPanelImg12.setLayout(jPanelImg12Layout);
        jPanelImg12Layout.setHorizontalGroup(
            jPanelImg12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg12Layout.setVerticalGroup(
            jPanelImg12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg12);

        jPanelImg13.setName("jPanelImg13"); // NOI18N

        javax.swing.GroupLayout jPanelImg13Layout = new javax.swing.GroupLayout(jPanelImg13);
        jPanelImg13.setLayout(jPanelImg13Layout);
        jPanelImg13Layout.setHorizontalGroup(
            jPanelImg13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg13Layout.setVerticalGroup(
            jPanelImg13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg13);

        jPanelImg14.setName("jPanelImg14"); // NOI18N

        javax.swing.GroupLayout jPanelImg14Layout = new javax.swing.GroupLayout(jPanelImg14);
        jPanelImg14.setLayout(jPanelImg14Layout);
        jPanelImg14Layout.setHorizontalGroup(
            jPanelImg14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg14Layout.setVerticalGroup(
            jPanelImg14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg14);

        jPanelImg15.setName("jPanelImg15"); // NOI18N

        javax.swing.GroupLayout jPanelImg15Layout = new javax.swing.GroupLayout(jPanelImg15);
        jPanelImg15.setLayout(jPanelImg15Layout);
        jPanelImg15Layout.setHorizontalGroup(
            jPanelImg15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg15Layout.setVerticalGroup(
            jPanelImg15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg15);

        jPanelImg16.setName("jPanelImg16"); // NOI18N

        javax.swing.GroupLayout jPanelImg16Layout = new javax.swing.GroupLayout(jPanelImg16);
        jPanelImg16.setLayout(jPanelImg16Layout);
        jPanelImg16Layout.setHorizontalGroup(
            jPanelImg16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        jPanelImg16Layout.setVerticalGroup(
            jPanelImg16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 82, Short.MAX_VALUE)
        );

        add(jPanelImg16);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Para cargar la imagen image pasada por parametro en la posicion index
     * de la grilla de imagenes que es de 4 x 4
     *
     * @param index
     * @param image
     */
    private void loadImage(int index, BufferedImage image, ImageItem imageItem) {
        try {
            panelImages[index].setLocation(1, 1);

            int h = image.getHeight();
            int w = image.getWidth();

            panelImages[index].setSize(w, h);

            for (Component comp: panelImages[index].getComponents()) {
                panelImages[index].remove(comp);
            }

            JSelectImagePanel panel = new JSelectImagePanel(image, 0, 0, panelImages[index].getSize(), imageItem);
            panel.addSelectImageListener(this);

            panelImages[index].setLayout(new BorderLayout());
            panel.setVisible(true);

            panelImages[index].add(panel);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void imageSelected(ImageItem image) {
        mainForm.editImage(image);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel9;
    private javax.swing.JPanel jPanelImg1;
    private javax.swing.JPanel jPanelImg10;
    private javax.swing.JPanel jPanelImg11;
    private javax.swing.JPanel jPanelImg12;
    private javax.swing.JPanel jPanelImg13;
    private javax.swing.JPanel jPanelImg14;
    private javax.swing.JPanel jPanelImg15;
    private javax.swing.JPanel jPanelImg16;
    private javax.swing.JPanel jPanelImg2;
    private javax.swing.JPanel jPanelImg3;
    private javax.swing.JPanel jPanelImg4;
    private javax.swing.JPanel jPanelImg5;
    private javax.swing.JPanel jPanelImg6;
    private javax.swing.JPanel jPanelImg7;
    private javax.swing.JPanel jPanelImg8;
    private javax.swing.JPanel jPanelImg9;
    // End of variables declaration//GEN-END:variables

}
